package model;

import java.rmi.RemoteException;
import java.util.HashMap;
import java.util.Map;

import javax.persistence.CascadeType;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.FetchType;
import javax.persistence.GeneratedValue;
import javax.persistence.Id;
import javax.persistence.ManyToOne;
import javax.persistence.MapKey;
import javax.persistence.OneToOne;
import javax.persistence.Table;

import org.hibernate.Hibernate;
import org.hibernate.annotations.CollectionOfElements;

import stub.Product;


@Entity
@Table( name = "order_entity" )
public class OrderImpl{
	
	@Id
	@GeneratedValue
	@Column( name = "order_id" )
	private Long					id;
	private boolean					orderSecured;
	@OneToOne( cascade = CascadeType.ALL )
	private CustomerImpl				customer;
	@CollectionOfElements(fetch=FetchType.EAGER)
	private Map<ProductImpl, Integer>	products;
	
	public OrderImpl() throws RemoteException{
		
	}
	
	public OrderImpl( CustomerImpl customer ) {
		this.customer = customer;
		products = new HashMap<ProductImpl, Integer>();
	}
	
	
	public void addProduct( ProductImpl p, int amount ) {
		if( products.containsKey( p ) ){
			products.put( p, products.get( p ) + amount );
		} else{
			products.put( p, amount );
		}
	}
	
	
	public Long getId() {
		return id;
	}
	
	
	public void setId( Long id ) {
		this.id = id;
	}
	
	
	public String getCustomerName() {
		return customer.getName();
	}
	
	
	public boolean secured() {
		return orderSecured;
	}
	
	
	public double getTotalPrice() throws RemoteException {
		double price = 0;
		products.values();
		for( Product p : products.keySet() ){
			price += p.getPrice() * products.get( p );
		}
		return price;
	}
	
	
	public Map<ProductImpl, Integer> getProductsOverview() {
		return products;
	}
	
	
	public boolean orderSecured() {
		orderSecured = true;
		return orderSecured;
	}
}
